<?
require_once(dirname(__FILE__).'/../models/Statuses.php');

class StatusesController extends Main_Controller_Action {	
	public function preDispatch()
    {
        if (!unserialize(Zend_Auth::getInstance()->getIdentity())->getIsAdministrator()) {        
                $this->_helper->redirector('', '');
        } else {
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
    
	function indexAction(){
		try{
			$orderBy = "ORDER BY sc_catalogue ASC, status ASC";	
			$statuses = ModelMapper::instance('Statuses')->findAll('', $orderBy);
		} catch (DatabaseException $e){
				$e->getMessage();
		}
			
		$this->view->statuses = $statuses;
	}
	
	function addAction(){
		$catalogues = ModelMapper::instance('StatusesCatalogues')->findAll();
		
		$smarty = $this->view->getEngine();
		
		$this->view->catalogues = $catalogues;
		$tpl = $smarty->fetch('statuses/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editAction(){
		$catalogues = ModelMapper::instance('StatusesCatalogues')->findAll();
		$status = ModelMapper::instance('Statuses')->find(intval($this->getRequest()->getParam('statusesId')));
		
		$smarty = $this->view->getEngine();
		
		$this->view->status = $status;
		$this->view->catalogues = $catalogues;
		$tpl = $smarty->fetch('statuses/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function saveAction(){
		$formData = $this->_request->getParam('addForm');
		
		if (intval($formData['statusesId']) >0)
			$status = ModelMapper::instance('Statuses')->find(intval($formData['statusesId']));
		else
			$status = new Statuses();
		
		$status->setStatus(trim(strip_tags(stripcslashes($formData['status']))));
		$status->setDescription(trim(strip_tags(stripcslashes($formData['description']))));
		$status->setStatusesCataloguesId(intval($formData['statusesCataloguesId']));

		exit();
	}
	
	function deleteAction(){
		$obj = ModelMapper::instance('Statuses')->find(intval($this->getRequest()->getParam('statusesId')));
		$obj->delete();

		exit();
	}
	
	/**
	 * STATUSES CATALOGUES SECTION
	 *
	 */
	function cataloguesAction(){
		try{
				$catalogues = ModelMapper::instance('StatusesCatalogues')->findAll();
		} catch (DatabaseException $e){
				$e->getMessage();
		}
			
		$this->view->catalogues = $catalogues;
	}
	
	function addCatalogueAction(){
		$smarty = $this->view->getEngine();
		
		$tpl = $smarty->fetch('statuses/add-catalogue.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editCatalogueAction(){
		//Database::instance()->getConn()->debug=1;
		$catalogue = ModelMapper::instance('StatusesCatalogues')->find(intval($this->getRequest()->getParam('statusesCataloguesId')));
		$this->view->c = $catalogue;
		
		$smarty = $this->view->getEngine();
		$tpl = $smarty->fetch('statuses/edit-catalogue.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
		
	}
	
	function saveCatalogueAction(){
		$formData = $this->_request->getParam('addForm');
		
		if (intval($formData['statusesCataloguesId']) >0){
			$obj = ModelMapper::instance('StatusesCatalogues')->find(intval($formData['statusesCataloguesId']));
		}
		else{
			$obj = new StatusesCatalogues();
		}
		
		$obj->setCatalogue(trim(strip_tags(stripcslashes($formData['catalogue']))));
			
		exit();
	}
	
	function deleteCatalogueAction(){
		$obj = ModelMapper::instance('StatusesCatalogues')->find(intval($this->getRequest()->getParam('statusesCataloguesId')));
		$obj->delete();

		exit();
	}
}

	
?>